FROM node:20-alpine AS development-dependencies-env
# 设置 npm 镜像源为淘宝源
RUN npm config set registry https://registry.npmmirror.com/
COPY . /app
WORKDIR /app
# 添加重试和超时配置
RUN npm ci --no-audit --no-fund --fetch-retries 5 --fetch-retry-mintimeout 20000 --fetch-retry-maxtimeout 120000

FROM node:20-alpine AS production-dependencies-env
RUN npm config set registry https://registry.npmmirror.com/
COPY ./package.json package-lock.json /app/
WORKDIR /app
RUN npm ci --omit=dev --no-audit --no-fund --fetch-retries 5 --fetch-retry-mintimeout 20000 --fetch-retry-maxtimeout 120000

FROM node:20-alpine AS build-env
COPY . /app/
COPY --from=development-dependencies-env /app/node_modules /app/node_modules
WORKDIR /app
RUN npm run build

FROM node:20-alpine
COPY ./package.json package-lock.json /app/
COPY --from=production-dependencies-env /app/node_modules /app/node_modules
COPY --from=build-env /app/build /app/build
WORKDIR /app
CMD ["npm", "run", "start"]